package com.speechb.common;

import org.apache.commons.lang.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.Period;
import java.util.*;

public class DateUtils {
   public static SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static  SimpleDateFormat ymdFormat=new SimpleDateFormat("yyyy-MM-dd");


    /**
     * 得到本周周一
     * 
     * @return yyyy-MM-dd
     */
    public static String getFormatThisWeekMonday(){
       Date date= getWeekMonday(new Date());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return  sdf.format(date);
    }

    public static Date getWeekMonday(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);		// 获得当前日期是一个星期的第几天
        int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
        ////      如果当前时间是星期天，则向上移动一天，再取本周的星期一，老外用周日到周六为一周，向前移动一天，则是中国人的本周
        if (1 == dayWeek) {			cal.add(Calendar.DAY_OF_MONTH, -1);
        }
        // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
        cal.setFirstDayOfWeek(Calendar.MONDAY);
        // 获得当前日期是一个星期的第几天
        int day = cal.get(Calendar.DAY_OF_WEEK);
        // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
        cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
        return cal.getTime();
    }

    /**
     * 获得当前日期，是星期几
     * 1 代表星期一 2 代表新星期二
     * @return
     */
    public static int dateToWeek() {
        Calendar cal = Calendar.getInstance(); // 获得一个日历
        Date datet = new Date();
            cal.setTime(datet);
        int w = cal.get(Calendar.DAY_OF_WEEK) -1; // 指示一个星期中的某天。
        if (w <=0)
            w = 7+Math.abs(w);
        return w;
    }


    /**
     * 得到格式化本周周日
     * 
     * @return yyyy-MM-dd
     */
    public static String getFormatSundayOfThisWeekStr() {
        SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-dd");
        return format.format(getSundayOfThisWeek());
    }

    /**
     *
     * 
     * @return yyyy-MM-dd hh:mm:ss
     */
    public static String formatNow() {
        return format.format(new Date());
    }
    /**
     * 得到格式化本周周日
     * 
     * @return Date
     */
    public static Date getSundayOfThisWeek() {
        Calendar c = Calendar.getInstance();
        int day_of_week = c.get(Calendar.DAY_OF_WEEK) - 1;
        if (day_of_week == 0)
            day_of_week = 7;
        c.add(Calendar.DATE, -day_of_week + 7);
        return c.getTime();
    }
    /**
     * 校验指定的时间是否合法
     * @param startTime
     * @param endTime
     * @return
     */
    public static boolean checkTimeValide(Date startTime, Date endTime) {
        Date date = new Date();
        if (startTime != null) {
            return date.after(startTime) && date.before(endTime);
        } else {
            return date.before(endTime);
        }
    }

    /**
     * 判断时间是否小于当前时间
     * @return true 小于当前时间 false 大于
     */
    public static boolean beforeDate(String endTime) throws ParseException {
        if (StringUtils.isEmpty(endTime)) {
            return false;
        }
        Date endDate = format.parse(endTime);
        return new Date().before(endDate);
    }

    public static String getFormatDataString(Date date){
        if(date==null){
            return "";
        }
        return format.format(date);
    }

    /**
     * 获得当前时间  yyyy-MM-dd
     * @return
     */
    public static String getCurrentFormatYYmmDD(){
        return ymdFormat.format(Calendar.getInstance().getTime());
    }

    /**
     * 获取本月第一天：
     * @return
     */
    public static String getMonthFirstDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return ymdFormat.format(calendar.getTime());
    }

    /**
     * 获取本月最后一天：
     * @return
     */
    public static String getMonthLastDay(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return ymdFormat.format(cal.getTime());
    }
    /**
     * 获取本月最后一天最后时刻
     * @return
     */
    public static String getMonthLastDayEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMaximum(Calendar.DAY_OF_MONTH));
        return ymdFormat.format(cal.getTime())+ " 23:59:59";
    }
    /**
     * 获取下个月第一天
     * @return
     */
    public static Date getNextMonthFirstDay(){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DAY_OF_MONTH,1);
        calendar.add(Calendar.MONTH,1);
        return calendar.getTime();
    }
    /**
     * 获取本年的第一天
     * @return String
     * **/
    public static String getYearStart(){
        return new SimpleDateFormat("yyyy").format(new Date())+"-01-01";
    }

    /**
     * 获取本年的最后一天
     * @return String
     * **/
    public static String getYearEnd(){
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH,calendar.getActualMaximum(Calendar.MONTH));
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date currYearLast = calendar.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(currYearLast)+" 23:59:59";
    }
    /**
     * 获取本周的最后一天
     * @return String
     * **/
    public static String getWeekEnd(){
        Calendar cal=Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, cal.getActualMaximum(Calendar.DAY_OF_WEEK));
        cal.add(Calendar.DAY_OF_WEEK, 1);
        Date time=cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time)+" 23:59:59";
    }

    /**
     * 增加个月
     * @param date 需要增加月的时间
     * @return 增加月后的日期
     * @throws ParseException 日期解析异常
     */
    public static String subMonth(String date, int monthCount) throws ParseException {
        Date dt = format.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.MONTH, monthCount);
        Date dt1 = rightNow.getTime();
        String reStr = format.format(dt1);
        return reStr;
    }

    /**
     * 增加个月
     * @param date 需要增加月的时间
     * @return 增加月后的日期
     * @throws ParseException 日期解析异常
     */
    public static String subYear(String date, int yearCount) throws ParseException {
        Date dt = format.parse(date);
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.YEAR, yearCount);
        Date dt1 = rightNow.getTime();
        String reStr = format.format(dt1);
        return reStr;
    }

    /**
     * 获取指定日期所在月份开始的时间戳
     * @param dateStr 指定日期
     * @return
     */
    public static String getMonthBegin(String dateStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(dateStr));

        //设置为1号,当前日期既为本月第一天
        c.set(Calendar.DAY_OF_MONTH, 1);
        //将小时至0
        c.set(Calendar.HOUR_OF_DAY, 0);
        //将分钟至0
        c.set(Calendar.MINUTE, 0);
        //将秒至0
        c.set(Calendar.SECOND,0);
        //将毫秒至0
        c.set(Calendar.MILLISECOND, 0);
        // 获取本月第一天的时间戳
        return format1.format(c.getTime()) + " 00:00:00";
    }
    /**
     * 获取指定日期所在月份结束的时间戳
     * @param dateStr 指定日期
     * @return
     */
    public static String getMonthEnd(String dateStr) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        c.setTime(format.parse(dateStr));

        //设置为当月最后一天
        c.set(Calendar.DAY_OF_MONTH, c.getActualMaximum(Calendar.DAY_OF_MONTH));
        //将小时至23
        c.set(Calendar.HOUR_OF_DAY, 23);
        //将分钟至59
        c.set(Calendar.MINUTE, 59);
        //将秒至59
        c.set(Calendar.SECOND,59);
        //将毫秒至999
        c.set(Calendar.MILLISECOND, 999);
        // 获取本月最后一天的时间戳
        return format1.format(c.getTime()) + " 23:59:59";
    }

    public static List<String> getLastTwelveMonths(){
        List<String> list = new ArrayList<>();
        Calendar c = Calendar.getInstance();
        String date = "";
        for(int i = 0; i < 12; i ++){
            int k = c.get(Calendar.YEAR);
            int j = c.get(Calendar.MONTH) + 1 - i;
            if(j >= 1){
                date = k + "-" + (j >= 10 ? "" : "0") + j;
            } else {
                int p = 11 - i;//剩余循环次数
                int m = c.get(Calendar.YEAR) - 1;
                int n = c.get(Calendar.MONTH) + 2 + p;
                date = m + "-" + (n >= 10 ? "" : "0") + n;
            }
            list.add(date);
        }
        return list;
    }

    public static void main(String [] args) {
        Random r = new Random(1);
        for (int i=0; i< 10;i++) {
            System.out.println(todayRadomTime());
        }
        System.out.println(dateToWeek());
    }

    /**
     * 2010-10-11 09:10:01取出 2010-10-11
     *
     * @return yyyy-MM-dd
     */
    public static String covertYMD(String dateStr) {
        if (StringUtils.isEmpty(dateStr)) {
            return "";
        }
        if (dateStr.length() < 10) {
            throw new IllegalArgumentException();
        }
        return dateStr.substring(0,10);
    }

    /**
     * 当天随机时间
     *
     * @return
     */
    public static String todayRadomTime() {
        Calendar calendar = Calendar.getInstance();
        Random r = new Random();
       // int currentHours = calendar.get(Calendar.HOUR_OF_DAY);
        calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY) - r.nextInt(8));
        calendar.set(Calendar.MINUTE, r.nextInt(60));
        calendar.set(Calendar.SECOND, r.nextInt(60));
        return format.format(calendar.getTime());
    }

    public static String expireTime(String timeStr) throws ParseException {
        if (StringUtils.isEmpty(timeStr)) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.getDefault());
        Date parse = format.parse(timeStr);
        calendar.setTime(parse);
        LocalDate from = LocalDate.of(calendar.get(Calendar.YEAR) ,calendar.get(Calendar.MONTH),calendar.get(Calendar.DAY_OF_MONTH));
        Period between = Period.between(LocalDate.now(), from);
        int distanceYear = between.getYears();
        int distanceMonth = between.getMonths();
        int distanceDay = between.getDays();

        StringBuilder sb = new StringBuilder();
        if (distanceYear >= 1) {
            sb.append(distanceYear);
            sb.append("年");
        }

        if (distanceMonth >= 1 || distanceYear >= 1) {
            sb.append(distanceMonth);
            sb.append("月");
        }

        sb.append(distanceDay);
        sb.append("天");
        return sb.toString();
    }

}
